/**
 * The TabPanel user-interface component class
 * @class syslib.ui.TabPanel
 * @author Gabriel Ricci
 */
syslib.ui.TabPanel = __class(syslib.ui.VisualTag, 
/** 
 * @lends syslib.ui.TabPanel
 */
{
	id : "", 
	contentBody : "", 
	tabs : "", 
	tabItens : [],
	head : {}, 
	body : {}, 

	prepare : function(element){
		// load the style
		syslib.loadStyleForComponent("tabs");

		// set the class attributes
		this.id = (__valid(this.__attributes.id) ? this.attributes.id : syslib.element.generateID());
		this.tabs = this.id + "_tabs_container";
		this.contentBody = this.id + "content_container";
		
		// return
		return true;
	},

	execute : function(){
		// tag attributes
		var attributes = syslib.object.extend(this.__attributes, {
			"class" : "tab-panel-main-container"
		});

		// element structure
		var structure = ["div", attributes,
			["div", {"innerName" : "head", "class" : "tab-panel-top-container", "id" : this.tabs}],
			["div", {"class" : "line"} ],
			["div", {"innerName" : "body", "class" : "tab-panel-content-container", "id" : this.contentBody}, 
				this.__innerHTML
			]
		];

		// create the element
		var tabPanel = this.createDOM(structure);
		delete structure;
		
		// set the data
		this.head = this.__refs.head;
		this.body = this.__refs.body;

		// return
		return tabPanel;
	},

	finalize : function(){},

	addTab : function(tabItem){
		if(__valid(tabItem) && typeof tabItem === 'object'){
			try{
				// create the index
				var index = this.tabItens.length;

				// store
				this.tabItens[index] = tabItem;
				
				// add the selector to the head
				this.head.appendChild(tabItem.selector);

				// select, if needed
				if(index == 0){
					this.selectTab(index);
				}
				
				// return
				return index;
			} catch(e){
				syslib.messages.error("syslib.ui.TabPanel.addTab(): Error adding the tab: " + e.message);
				return false;
			}
		}
	},

	selectTab : function(index){
		// search the tabitens
		for(var i in this.tabItens){
			// get the tabitem
			var tabItem = this.tabItens[i];
			
			// get the attributes
			var attributes	= (__valid(tabItem.__attributes)) ? tabItem.__attributes : {};
			
			// get the content
			var content	= tabItem.content;

			// check the data integrity
			if(!__valid(attributes) || !__valid(content)){
				syslib.messages.error("syslib.ui.TabPanel.selectTag: Invalid tab index: " + i);
				return false;
			}

			// check if this tab is the tab that must be selected
			if(i == index){
				// check if there's a page to load
				if(__valid(tabItem.__attributes.page)){
					// clear the container
					syslib.element.update(content, '');
					
					// create a new ajax request
					new syslib.ajax.request(tabItem.__attributes.page, {
						destiny : content,
						store : false
					});
				}
				
				// show the content
				syslib.element.show(content);
				
				// set the tab as selected
				tabItem.__select();
			} else {
				// hide the content
				syslib.element.hide(content);
				
				// set the tab as unselected
				tabItem.__unselect();
			}
		}
	}
});